<template>
  <div id="script" class="script-container">
    <i-button type="primary" @click="handleOperation('add')" class="script-add"
      >添加</i-button
    >
    <el-table :data="scriptData" class="script-table" border>
      <el-table-column type="index" label="序号" align="center" width="80">
      </el-table-column>
      <el-table-column
        v-for="(item, index) in scriptColumns"
        :key="index"
        :prop="item.key"
        :label="item.title"
        align="center"
        :width="item.width ? item.width : ''"
      ></el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button @click="handleOperation('edit',scope.$index, scope.row)" type="text" size="small"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="handleDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
module.exports = {
  props: {
    title: {
      type: String,
    },
  },
  data: function () {
    return {
      scriptColumns: [
        {
          title: "脚本名称",
          key: "name",
          align: "center",
        },
        {
          title: "说明",
          key: "remark",
          align: "center",
        },
        {
          title: "文件名",
          key: "orgName",
          align: "center",
        },
      ], //统计脚本列
      scriptData: [],
      dictModel: {
        scripitName: "",
        explain: "",
        fileName: "",
      },
    };
  },
  created(){
    localStorage.removeItem("temp")
  },
  mounted(){
    this.getScriptList()
  },
  methods: {
    handleOperation(type,index,row) {
      this.$parent.$parent.activeName = this.$parent.$parent.menuModel = 'scriptEdit'
      localStorage.setItem("temp", JSON.stringify(row || {}));
    },
    getScriptList() {
      $http.get({
        url:api.scriptList,
        data:{
          token:this.token,
        },
        success:(result,data)=>{
          if(data.code == 200) {
            this.scriptData = data.rows
          }
        },
        error:(err)=>{

        }
      },this)
    },
    handleDel(row) {
      this.$confirm('此操作将永久删除该脚本, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        $http.post({
          url:api.scriptDelete,
          contentType:'json',
          data:{
            token:this.token,
            ids: [row.id],
          },
          success:(result,data)=>{
            if(data.code == 200) {
              setTimeout(()=>{
                this.$message({
                  type: 'success',
                  message: data.msg
                });
                this.getScriptList();
              },500)

            }else {
              this.$message({
                type: 'info',
                message: data.msg
              });
            }

          },
          error:(err)=>{

          }
        },this)

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  },
};
</script>

<style>
</style>
